package Logic

import (
	// _ "gPublic/grpc/pubData/go"
	pb "gPublic/grpc/yz-pub-data/pubData"
)

// type Ct pb.Content
// type WebMenu pb.MenuData
// type WebMenus pb.MenuItem

// 接收指针类型参数
func GetMenuTree(menus []*pb.MenuItem) pb.MenuData {
	var data []*pb.MenuItem // 定义类型
	for _, menu := range menus {
		if menu.ParentId == 0 {
			data = append(data, GetChildren(menus, menu)) // 因为传进来的已经是指针，所以这里直接用 不用&去传地址
		}
		data = append(data, menu)
	}

	return pb.MenuData{MenuItems: data}
}

// 定义参数是指针类型，因为grpc生成的树形结构体 是一个切片指针类型
func GetChildren(menus []*pb.MenuItem, menu *pb.MenuItem) *pb.MenuItem {
	var c []*pb.MenuItem // 定义类型
	for _, child := range menus {
		if int32(menu.Id) == int32(child.ParentId) {
			c = append(c, GetChildren(menus, child))
		}
	}

	menu.Children = c
	return menu
}

func GetMenuTreeLogic() []*pb.MenuData {
	ct := &pb.Content{Link: "Content"}
	// ct := Ct{Link: "Content"}

	// 构建示例数据
	menus := []*pb.MenuItem{
		{Id: 1,
			ParentId:       0,
			Title:          "Title",
			MenuType:       "MenuType",
			Content:        ct,
			SeoKeywords:    "SeoKeywords",
			SeoDescription: "SeoDescription"},
		{Id: 2,
			ParentId:       0,
			Title:          "Title",
			MenuType:       "MenuType",
			Content:        ct,
			SeoKeywords:    "SeoKeywords",
			SeoDescription: "SeoDescription"},
		{Id: 3,
			ParentId:       1,
			Title:          "Title",
			MenuType:       "MenuType",
			Content:        ct,
			SeoKeywords:    "SeoKeywords",
			SeoDescription: "SeoDescription"},
		{Id: 4,
			ParentId:       0,
			Title:          "Title",
			MenuType:       "MenuType",
			Content:        ct,
			SeoKeywords:    "SeoKeywords",
			SeoDescription: "SeoDescription"},
		{Id: 5,
			ParentId:       0,
			Title:          "Title",
			MenuType:       "MenuType",
			Content:        ct,
			SeoKeywords:    "SeoKeywords",
			SeoDescription: "SeoDescription"},
		{Id: 6,
			ParentId:       0,
			Title:          "Title",
			MenuType:       "MenuType",
			Content:        ct,
			SeoKeywords:    "SeoKeywords",
			SeoDescription: "SeoDescription"},
	}

	menuTree := GetMenuTree(menus)

	// return menuTree
	return []*pb.MenuData{&menuTree}
	// 打印菜单树
	// fmt.Printf("%+v\n", menuTree)
}
